#!/bin/bash
. ./lib

options=()
options+=("Config" "/etc/mkinitcpio.conf")
options+=("mkinicpio" "")

defaultitem="Config"
sel=$(whiptail --backtitle "$apptitle" --title "Initcpio Config Menu :" --menu "" --default-item "$defaultitem" --cancel-button "Back" 0 0 0 \
  "${options[@]}" \
  3>&1 1>&2 2>&3)
if [ ! "$?" = "0" ]; then
  exit 1
fi
sed -i "/^defaultitem=/c\defaultitem=\"$sel\"" $0

case $sel in
  'Config') editfile /etc/mkinitcpio.conf;;
  'mkinicpio')
    options=()
    configs=$(ls /etc/mkinitcpio.d)
    for itm in $configs; do
      options+=("${itm::-7}" "" on)
    done
    sel=$(whiptail --backtitle "$apptitle" --title "Choose configs :" --checklist "" --cancel-button "Back" 0 0 0 \
      "${options[@]}" \
      3>&1 1>&2 2>&3)
    if [ ! "$?" = "0" ]; then
      exit 1
    fi
    clear
    for itm in $sel; do
      case $itm in
        *)
          itm=$(sed "s/\"//g" <<<"$itm")
          echo "mkinitcpio -p $itm"
          mkinitcpio -p $itm
        ;;
      esac
    done
    pressanykey
  ;;
esac

exit 0
